package com.components.animation
{
	/**
	 * @auth Chunning
	 * @purpose could create animation, could dispathch events
	 * @date 2009-3-18
	 * */		
	import flash.display.MovieClip;
	import flash.events.Event;
	
	
	[Event(name="played",type="com.components.animation.AnimationEvents")]
	[Event(name="over",type="com.components.animation.AnimationEvents")]
	[Event(name="playing",type="com.components.animation.AnimationEvents")]
	
	public class Animation implements I_Animation
	{
		//START
		
		//PROPERTY
			
		private var repeat:uint;
		
		private var played:uint;
		
		private var isyoyo:Boolean;
		
		public var movie:MovieClip;
		
		//CONSTRUCTOR
		public function Animation(target:MovieClip,autoPlay:Boolean = false,repeat:uint = 1)
		{
						
			this.repeat = repeat;
			
			this.movie = target;
			
			this.movie.mouseEnabled = false;
			
			this.movie.mouseChildren = false;	
			
			if(autoPlay)
			{
				
				this.movie.play();
				
			}else{
				
				this.movie.stop();
				
			}
			
			this.movie.addEventListener(Event.ENTER_FRAME,onEnterFrame);
				
		}
		
		//METHODS
		
		//onEnterFrame handler		
		private function onEnterFrame(evt:Event):void
		{
			
			this.dispatchEvent(new AnimationEvents(AnimationEvents.PLAYING));
			
			if(this.isyoyo)
			{
				
				if(this.movie.currentFrame == 1)
				{
					
					this.movie.stop();
					
					this.movie.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
					
					this.movie.dispatchEvent(new AnimationEvents(AnimationEvents.OVER,true));
					
				}else{
				
					var nextFrame:uint = this.movie.currentFrame - 1;				
				
					this.movie.gotoAndStop(nextFrame);
				
				}
				
				return;			
				
			}
			
			if(this.movie.currentFrame == this.movie.totalFrames)
			{
				
				this.played++;
				
				this.movie.dispatchEvent(new AnimationEvents(AnimationEvents.PLAYED,true));
								
			}
			
			if(this.played == this.repeat)
			{
								
				this.movie.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
					
				this.movie.stop();
				
				this.played = 0;
				
				this.movie.dispatchEvent(new AnimationEvents(AnimationEvents.OVER,true));		
				
			}
			
			
		}
		
		//reset the animation
		public function reset():void
		{
			
			this.played = 0;
			
			this.movie.gotoAndStop(this.isyoyo ? this.movie.totalFrames : 1);
					
								
		}
		
		//yoyoyoyo....haha...
		public function yoyo():void
		{
									
			this.isyoyo = true;
			
			this.reset();
			
			if(!this.movie.hasEventListener(Event.ENTER_FRAME))
			{
				this.movie.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
						
		} 
		
		//play the movie
		public function play():void
		{
			
			this.isyoyo = false;
			
			this.reset();
				
			this.movie.play();
			
			if(!this.movie.hasEventListener(Event.ENTER_FRAME))
			{
				this.movie.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
			
		}		
		
		//stop animation...
		public function stop():void
		{
			
			this.reset();
			
			if(this.movie.hasEventListener(Event.ENTER_FRAME))
			{
				this.movie.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
			
		}
		
		//pause...
		public function pause():void
		{
			
			this.movie.stop();
			
			if(this.movie.hasEventListener(Event.ENTER_FRAME))
			{
			
				this.movie.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			}
			
		}
		
		//resume...
		public function resume():void
		{
			
			this.movie.play();
			
			if(!this.movie.hasEventListener(Event.ENTER_FRAME))
			{
			
				this.movie.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			}
			
		}
		
		//set movie visible...
		public function set visible(val:Boolean):void
		{
			
			this.movie.visible = val;
			
		}
		
		//get movie visible...
		public function get visible():Boolean
		{
		
			return this.movie.visible;	
			
		}
		
		
		//addeventlistener
		public function addEventListener(type:String,listener:Function,useCapture:Boolean = false,priority:int = 0,useWeakReference:Boolean = false):void
		{
			this.movie.addEventListener(type,listener,useCapture,priority,useWeakReference);	
		}
		
		//dispathch
		public function dispatchEvent(evt:Event):Boolean
		{
			
			return this.movie.dispatchEvent(evt);
			
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			
			this.movie.removeEventListener(type,listener,useCapture);
			
		}
				
		public function hasEventListener(type:String):Boolean
		{
			return this.movie.hasEventListener(type);
		}
		
		
		public function willTrigger(type:String):Boolean
		{
			return this.movie.willTrigger(type);
		}
		
				
		//END
	}
}